﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using KingRoad.EPM.Web.Util;
using DotNet.Service;
using DotNet.Utilities;
using DotNet.Model.MyEntity;
//using KingRoad.EPM.InvestmeEntity;
using KingRoad.EPM.Web.ViewModels;


namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    ///  工作组管理
    /// 功能描述：工作组增删改，工作组内的相关人员维护
    /// 模块负责人：WS
    /// 完成日期：2012.3.13
    /// </summary>
    public class WorkgroupController : Controller
    {
        //
        // GET: /Permission/Workgroup/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取所有工作组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetWorkgroupNodes(int id = 0)
        {
            List<Base_Workgroup> list = null;
            if (id == 0)
            {
                list = ServiceManager.Instance.BaseWorkgroupService.GetAllWorkgroup();
            }
            list.Add(new Base_Workgroup { Id = 0, ParentId = -1, Name = "工作组" });
            return Json(list);
        }

        /// <summary>
        /// 创建工作组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CreateWorkgroup(string id)
        {
            ViewBag.ParentID = id;
            if (String.IsNullOrEmpty(id)) ViewBag.ParentID = "0";
            return View();
        }

        /// <summary>
        /// POST 
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public ActionResult WorkgroupCreating(Base_Workgroup detail)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            string callbackType = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                if (detail.Id == 0)
                {
                    callbackType = "Create";
                    detail.DeletionStateCode = 0;
                    string id = ServiceManager.Instance.BaseWorkgroupService.AddWorkgroup(detail, out statuCode, out statuMessage);
                    //返回岗位实体添加树节点
                    detail.Id = int.Parse(id);
                }
                else
                {
                    callbackType = "Edit";
                    var baseDetail = ServiceManager.Instance.BaseWorkgroupService.GetEntity(UserService.CurrUserInfo, detail.Id.ToString());
                    baseDetail.Name = detail.Name;
                    baseDetail.Description = detail.Description;
                    ServiceManager.Instance.BaseWorkgroupService.Update(UserService.CurrUserInfo, baseDetail, out statuCode, out statuMessage);
                }
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = (statuCode == StatusCode.OKAdd.ToString() || statuCode == StatusCode.OKUpdate.ToString()) ? "200" : "300",
                callbackType = callbackType,
                data = detail
            });
        }
        /// <summary>
        /// 编辑工作组 WS 2012.4.19
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditWorkgroup(string id)
        {
            Base_Workgroup curDetail = null;
            var detail = ServiceManager.Instance.BaseWorkgroupService.GetEntity(UserService.CurrUserInfo,id);
            if(detail!=null)
            {
               curDetail =AutoMapper.Mapper.DynamicMap<Base_Workgroup>(detail);
            }
            return View(curDetail);
        }

        /// <summary>
        /// 软删除工作组，并移除所有职员关系
        /// </summary>
        /// <param name="workgroupId"></param>
        /// <returns></returns>
        public ActionResult DelWorkgroup(string id)
        {
            string outMessage = string.Empty, statusCode = string.Empty;
            List<Base_Workgroup> list = ServiceManager.Instance.BaseWorkgroupService.GetAllWorkgroup();
            int count = (from w in list where w.ParentId.Value.ToString() == id select w).Count();
            if (count > 0)
            {
                return Json(new CallbackStatus
                {
                    message = "该工作组下含有子工作组！",
                    statusCode = "300",
                    callbackType = "close"
                });
            }
            if (ServiceManager.Instance.BaseWorkgroupService.DelWorkgroup(id) > 0)
            {
                outMessage = "成功删除该工作组！";
                statusCode = "200";
            }
            else
            {
                outMessage = "删除工作组失败！";
                statusCode = "300";
            }
            return Json(new CallbackStatus
            {
                message = outMessage,
                statusCode = statusCode,
                callbackType = "close"
            });
        }

        /// <summary>
        /// 弹出组织机构职员树，并选择职员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SelectStaff(int id)
        {
            ViewBag.Flag = "7";
            ViewBag.WorkgroupId = id;
            return View();
        }

        /// <summary>
        /// 加入职员到工作组内
        /// </summary>
        /// <param name="workgroupId"></param>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public ActionResult CreateConnectionWorkgroupAndStaff(string workgroupId, string staffId)
        {
            string outMessage = string.Empty, statusCode = string.Empty;
            if (ServiceManager.Instance.BaseWorkgroupService.CreateConnectionWorkgroupAndStaff(workgroupId, staffId) > 0)
            {
                outMessage = "成功加入职员！";
                statusCode = "200";
            }
            else
            {
                outMessage = "加入职员失败！";
                statusCode = "300";
            }
            return Json(new CallbackStatus
            {
                message = outMessage,
                statusCode = statusCode,
                callbackType = "close"
            });
        }

        /// <summary>
        /// 从某个工作组中移除职员
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="workgroupId"></param>
        /// <returns></returns>
        public ActionResult RemoveFromWorkgroup(string staffId, string workgroupId)
        {
            string outMessage = string.Empty, statusCode = string.Empty;
            if (ServiceManager.Instance.BaseWorkgroupService.RemoveConnectionWorkgroupAndStaff(workgroupId, staffId) > 0)
            {
                outMessage = "成功移除职员！";
                statusCode = "200";
            }
            else
            {
                outMessage = "移除职员失败！";
                statusCode = "300";
            }
            return Json(new CallbackStatus
            {
                message = outMessage,
                statusCode = statusCode,
                callbackType = "close"
            });
        }

        /// <summary>
        /// 获取某个工作组下的所有职员
        /// </summary>
        /// <param name="queryBase"></param>
        /// <param name="workgroupId"></param>
        /// <returns></returns>
        public ActionResult GetStaffsByWorkgroupId(QueryBase queryBase, string workgroupId)
        {
            var list= ServiceManager.Instance.BaseWorkgroupService.GetStaffsByWorkgroupId(workgroupId);
            var jqGridData = WebUtility.TojqGridData(list, queryBase);
            return Json(jqGridData);
        }
    }
}
